package com.RedRiver.hospitalsystem.finance_service.service.impl;

import cn.hutool.core.date.DateTime;
import com.RedRiver.hospitalsystem.finance_service.common.WoniuxyBeanCopyUtil;
import com.RedRiver.hospitalsystem.finance_service.common.exception.BillException;
import com.RedRiver.hospitalsystem.finance_service.infra.mapper.BillDetailMapper;
import com.RedRiver.hospitalsystem.finance_service.infra.mapper.BillMapper;
import com.RedRiver.hospitalsystem.finance_service.infra.model.Bill;
import com.RedRiver.hospitalsystem.finance_service.infra.model.BillDetail;
import com.RedRiver.hospitalsystem.finance_service.service.BillDetailService;
import com.RedRiver.hospitalsystem.finance_service.service.info.FindBillDetailInfo;
import com.RedRiver.hospitalsystem.finance_service.service.info.FindBillDetailInfoList;
import com.RedRiver.hospitalsystem.finance_service.service.info.FindBillDetailInfoXiList;
import com.RedRiver.hospitalsystem.finance_service.service.info.FindBillDetailXiInfo;
import com.RedRiver.hospitalsystem.medicine_service.client.MedicineClient;
import com.RedRiver.hospitalsystem.medicine_service.client.param.FindMedicineByIdInfoClient;
import com.RedRiver.hospitalsystem.pathology_service.client.CheckProjectClient;
import com.RedRiver.hospitalsystem.pathology_service.client.result.SearchCheckProjectResult;
import com.RedRiver.hospitalsystem.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 龙世超
 * @since 2022-04-26
 */
@Service
public class BillDetailServiceImpl extends ServiceImpl<BillDetailMapper, BillDetail> implements BillDetailService {
    @Resource
    private BillMapper billMapper;
    @Resource
    private BillDetailMapper billDetailMapper;
    @Resource
    private MedicineClient medicineClient;
    @Resource
    private CheckProjectClient checkProjectClient;
    @Override
    public FindBillDetailInfoList findBillDetailById(Integer id) {
        QueryWrapper<BillDetail> wrapper = new QueryWrapper<>();
        QueryWrapper<BillDetail> billId = wrapper.eq("bill_id", id);
        List<BillDetail> list = billDetailMapper.selectList(billId);
        FindBillDetailInfoList infoList = new FindBillDetailInfoList();
        ArrayList<FindBillDetailInfo> infos = new ArrayList<>();
        if (list!=null){
            for (BillDetail billDetail : list) {
                FindBillDetailInfo copyone = WoniuxyBeanCopyUtil.copyone(billDetail, FindBillDetailInfo.class);
                infos.add(copyone);
            }
        }
        infoList.setList(infos);
        return infoList;
    }
    @Override
    public FindBillDetailInfoXiList findBillDetailByIdXi(Integer id) {
        QueryWrapper<BillDetail> wrapper = new QueryWrapper<>();
        QueryWrapper<BillDetail> billId = wrapper.eq("bill_id", id);
        List<BillDetail> list = billDetailMapper.selectList(billId);
        FindBillDetailInfoXiList infoList = new FindBillDetailInfoXiList();
        ArrayList<FindBillDetailXiInfo> infos = new ArrayList<>();
        if (list!=null){
            for (BillDetail billDetail : list) {
                FindBillDetailXiInfo copyone = WoniuxyBeanCopyUtil.copyone(billDetail, FindBillDetailXiInfo.class);
                Bill bill = billMapper.selectById(copyone.getBillId());
                if (null!=bill){
                    if (bill.getPaymentId()==1){
                        Result<FindMedicineByIdInfoClient> medicineById = medicineClient.findMedicineById(copyone.getTypeId());
                        if (medicineById.getCode()==200&&medicineById.getData()!=null) {
                            copyone.setTypeName(medicineById.getData().getMedicineName());
                        }else throw new BillException(50001,"该药物id未找到任何数据");
                    }
                    else if (bill.getPaymentId()==2){
                        //查询检查项名，如上if
                        Result<SearchCheckProjectResult> result = checkProjectClient.searchCheckProjectById(copyone.getTypeId());
                        if (result.getCode()==200&&result.getData()!=null){
                            copyone.setTypeName(result.getData().getCheckProjectName());
                        }else throw new BillException(50001,"该检查id未找到任何数据");
                    }
                }
                infos.add(copyone);
            }
        }
        infoList.setList(infos);
        return infoList;
    }
}
